package cn.itrip.auth.controller;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.itrip.auth.service.toKen.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.ErrorCode;

/**
 * Token控制器
 * @author hduser
 *
 */
@Controller
@Api
@RequestMapping(value = "/api")
public class TokenController {

	@Resource
	private TokenService tokenService;
	/**
	 * 置换token
	 * 
	 * @return 新的token信息
	 */
	@ApiOperation(value="客户端置换token", httpMethod = "POST",
			protocols="HTTP", produces = "application/json",
			response=Dto.class,
			notes="提供客户端置换token操作，服务器需要获取客户端header中的token串")
	@ApiImplicitParam(paramType="header",required=true,name="token",value="用户认证凭据",defaultValue="token:PC-21ec64d6e9cae0917ea4b54bc36809d8-8-20170601100121-699dfc")
	@RequestMapping(value="/retoken", method=RequestMethod.POST,produces="application/json")
	public @ResponseBody Dto replace(HttpServletRequest request) {
		 String agent = request.getHeader("user-agent");
		 String toKen = request.getHeader("toKen");
		 try{
		 	String newToken = tokenService.replaceToken(agent,toKen);
			 ItripTokenVO tokenVO = new ItripTokenVO(toKen,Calendar.getInstance().getTimeInMillis()+TokenService.SESSION_TIMEOUT*1000,Calendar.getInstance().getTimeInMillis());
			 return DtoUtil.returnDataSuccess(tokenVO);
		 }catch (Exception e){
		 	e.printStackTrace();
			 return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_TOKEN_INVALID);
		 }
	}
}
